﻿using MyWebServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace MyWebServer
{
    public class Room
    {
        public Queue<int> queue_pai=new Queue<int>(136);
        public int Jushu = 4;
        public bool isKechi = true;
        public int RoomName = 0;
        public int duiZhanJvShu = 0;
        public int yvPaiJvShu = 0;
        public int AllActorCount=0;
        public int masterClientID = 0;
        public int chupaishuzi = 0;
        public int chuPaiID = 0;
        public int needChuPaiID;
        public int diFen = 0;
        public int anGangJiaGangID = 0;
        public List<Desk> desklist=new List<Desk>();
        public RoomInfo roomInfo = new RoomInfo();
        public bool isExcutedChuPaiDataReturn = false;
        public bool isExcutedQiangGangDataReturn = false;
        public bool isChuPaiReturn = true;
        public bool isganghouchupai = false;
        public Desk desk_dong = new Desk() { playerPosition = PlayerPosition.dong };
        public Desk desk_nan = new Desk() { playerPosition = PlayerPosition.nan };
        public Desk desk_xi = new Desk() { playerPosition = PlayerPosition.xi };
        public Desk desk_bei = new Desk() { playerPosition = PlayerPosition.bei };
        public List<HuReturnData> list_huReturn = new List<HuReturnData>();
        public List<PengGangReturnData> list_pgReturn = new List<PengGangReturnData>();
        public List<ChiReturnData> list_chiReturn = new List<ChiReturnData>();
        public List<QiangGangReturnData> list_qiangGangReturn = new List<QiangGangReturnData>();
        /// <summary>
        /// 等待出牌返回计时
        /// </summary>
        public Timer timer_ChuPaiReturnJiShi = new Timer(12000);

        /// <summary>
        /// 等待抢杠返回计时
        /// </summary>
        public Timer timer_QiangGangReturnJiShi = new Timer(12000);

        /// <summary>
        /// 需要出牌计时
        /// </summary>
        public Timer timer_NeedChuPaiJiShi = new Timer(12000);

        /// <summary>
        /// 重复更新牌
        /// </summary>
        public Timer timer_updatePai = new Timer(2000);
        public Room()
        {
            desklist.Add(desk_dong);
            desklist.Add(desk_nan);
            desklist.Add(desk_xi);
            desklist.Add(desk_bei);
            timer_ChuPaiReturnJiShi.Elapsed += OnTimerOver_ChuPaiReturn;
            timer_ChuPaiReturnJiShi.AutoReset = false;
            timer_QiangGangReturnJiShi.Elapsed += OnTimerOver_QiangGangReturn;
            timer_QiangGangReturnJiShi.AutoReset = false;
            timer_NeedChuPaiJiShi.Elapsed += OnTimerOver_NeedChuPai;
            timer_NeedChuPaiJiShi.AutoReset = false;
        }
        public void ChuPaiDataReturn()
        {
            Console.WriteLine("isExcutedChuPaiDataReturn现在是" + isExcutedChuPaiDataReturn);
            if (isExcutedChuPaiDataReturn == false)
            {
                isExcutedChuPaiDataReturn = true;
                timer_ChuPaiReturnJiShi.Stop();
                if (list_huReturn.Count > 0)
                {
                    Dictionary<short, object> dict = new Dictionary<short, object>();
                    dict.Add(1, chupaishuzi);
                    dict.Add(2, chuPaiID);
                    dict.Add(3, list_huReturn);
                    dict.Add(4, desk_dong.shoupai);
                    dict.Add(5, desk_nan.shoupai);
                    dict.Add(6, desk_xi.shoupai);
                    dict.Add(7, desk_bei.shoupai);
                    JieSuan jiesuan = new JieSuan();
                    //如果是约牌局
                    if (roomInfo.isYuePai)
                    {
                        //开始结算分数                    
                        int lostFenshu = 0;
                        for (int i = 0; i < list_huReturn.Count; i++)
                        {
                            int tempfenshu = jiesuan.GetHuScore(roomInfo.roomType, list_huReturn[i].hplx, 1);

                            lostFenshu += tempfenshu;

                            int hupaiID = list_huReturn[i].hupaiActorID;

                            for (int j = 0; j < desklist.Count; j++)
                            {
                                if (desklist[j].peer.player.actorId == hupaiID)
                                {
                                    desklist[j].peer.player.jiFen += tempfenshu;
                                }
                            }
                        }

                        //2.再更新输玩家，或放炮玩家的分数
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            if (chuPaiID == desklist[i].peer.player.actorId)
                            {
                                desklist[i].peer.player.jiFen -= lostFenshu;
                                break;
                            }
                        }

                        //保存每个桌子上的积分
                        desk_dong.jiFen = desk_dong.peer.player.jiFen;
                        desk_nan.jiFen = desk_nan.peer.player.jiFen;
                        desk_xi.jiFen = desk_xi.peer.player.jiFen;
                        desk_bei.jiFen = desk_bei.peer.player.jiFen;

                        int jiFen_dong = desk_dong.peer.player.jiFen;
                        int jiFen_nan = desk_nan.peer.player.jiFen;
                        int jiFen_xi = desk_xi.peer.player.jiFen;
                        int jiFen_bei = desk_bei.peer.player.jiFen;

                        dict.Add(8, jiFen_dong);
                        dict.Add(9, jiFen_nan);
                        dict.Add(10, jiFen_xi);
                        dict.Add(11, jiFen_bei);
                    }
                    else
                    {
                        int lostFenshu = 0;
                        for (int i = 0; i < list_huReturn.Count; i++)
                        {
                            int tempfenshu = jiesuan.GetHuScore(roomInfo.roomType, list_huReturn[i].hplx, 1);

                            lostFenshu += tempfenshu;

                            int hupaiID = list_huReturn[i].hupaiActorID;

                            for (int j = 0; j < desklist.Count; j++)
                            {
                                if (desklist[j].peer.player.actorId == hupaiID)
                                {
                                    int change = tempfenshu;
                                    MySQLManager.UpdateCoin(change, desklist[j].peer.userinfo.id);
                                }
                            }
                        }

                        //2.再更新输玩家，或放炮玩家的分数
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            if (chuPaiID == desklist[i].peer.player.actorId)
                            {
                                int change =lostFenshu;
                                MySQLManager.UpdateCoin(-change, desklist[i].peer.userinfo.id);
                                break;
                            }
                        }
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            MySQLManager.UpdatejinriCoinJushu(1,desklist[i].peer.userinfo.id);
                            MySQLManager.UpdatejinriZongJushu(1,desklist[i].peer.userinfo.id);
                        }
                        //保存每个桌子上的积分
                        desk_dong.jiFen = desk_dong.peer.player.jiFen;
                        desk_nan.jiFen = desk_nan.peer.player.jiFen;
                        desk_xi.jiFen = desk_xi.peer.player.jiFen;
                        desk_bei.jiFen = desk_bei.peer.player.jiFen;

                        int jiFen_dong = desk_dong.peer.player.jiFen;
                        int jiFen_nan = desk_nan.peer.player.jiFen;
                        int jiFen_xi = desk_xi.peer.player.jiFen;
                        int jiFen_bei = desk_bei.peer.player.jiFen;

                        int coin_dong = MySQLManager.UseIDfindCoin(desk_dong.peer.userinfo.id);
                        int coin_nan = MySQLManager.UseIDfindCoin(desk_nan.peer.userinfo.id);
                        int coin_xi = MySQLManager.UseIDfindCoin(desk_xi.peer.userinfo.id);
                        int coin_bei = MySQLManager.UseIDfindCoin(desk_bei.peer.userinfo.id);

                        dict.Add(8, jiFen_dong);
                        dict.Add(9, jiFen_nan);
                        dict.Add(10, jiFen_xi);
                        dict.Add(11, jiFen_bei);
                        dict.Add(13, coin_dong);
                        dict.Add(14, coin_nan);
                        dict.Add(15, coin_xi);
                        dict.Add(16, coin_bei);
                    }
                    dict.Add(12, roomInfo);
                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (desklist[i].peer.IsConnected && desklist[i].peer != null)
                        {
                            desklist[i].peer.SendEvent(10, dict);
                        }
                    }
                    OnGameOver();
                    desk_dong.peer.gamestate = GameState.score;
                    desk_nan.peer.gamestate = GameState.score;
                    desk_xi.peer.gamestate = GameState.score;
                    desk_bei.peer.gamestate = GameState.score;
                }
                else if (list_pgReturn.Count > 0)
                {
                    Dictionary<short, object> Chidict = new Dictionary<short, object>();
                    Chidict.Add(1, chupaishuzi);
                    Chidict.Add(2, chuPaiID);
                    Chidict.Add(3, list_pgReturn[0].pengGangActorID);
                    Chidict.Add(4, list_pgReturn[0].pglx);

                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (desklist[i].peer.IsConnected && desklist[i].peer != null)
                        {
                            desklist[i].peer.SendEvent(13, Chidict);
                        }
                    }
                    if(list_pgReturn[0].pglx == PengGangLeixing.Peng)
                    {
                        needChuPaiID = list_pgReturn[0].pengGangActorID;
                        isganghouchupai = false;
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            if (desklist[i].peer != null && desklist[i].peer.IsConnected)
                            {
                                desklist[i].peer.SendEvent(19, null);//开启计时功能
                            }
                        }
                    }
                    int pgID = list_pgReturn[0].pengGangActorID;
                    if (pgID == desk_dong.peer.player.actorId&& list_pgReturn[0].pglx==PengGangLeixing.Peng)
                    {
                        desk_dong.shoupai.Remove(chupaishuzi);
                        desk_dong.shoupai.Remove(chupaishuzi);
                        desk_dong.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Peng, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_dong.shoupai.Contains(0))
                        {
                            desk_dong.shoupai.Remove(0);
                        }
                        timer_NeedChuPaiJiShi.Start();
                    }
                    else if(pgID == desk_nan.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Peng)
                    {
                        desk_nan.shoupai.Remove(chupaishuzi);
                        desk_nan.shoupai.Remove(chupaishuzi);
                        desk_nan.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Peng, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_nan.shoupai.Contains(0))
                        {
                            desk_nan.shoupai.Remove(0);
                        }
                        timer_NeedChuPaiJiShi.Start();
                    }
                    else if(pgID == desk_xi.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Peng)
                    {
                        desk_xi.shoupai.Remove(chupaishuzi);
                        desk_xi.shoupai.Remove(chupaishuzi);
                        desk_xi.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Peng, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_xi.shoupai.Contains(0))
                        {
                            desk_xi.shoupai.Remove(0);
                        }
                        timer_NeedChuPaiJiShi.Start();
                    }
                    else if (pgID == desk_bei.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Peng)
                    {
                        desk_bei.shoupai.Remove(chupaishuzi);
                        desk_bei.shoupai.Remove(chupaishuzi);
                        desk_bei.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Peng, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_bei.shoupai.Contains(0))
                        {
                            desk_bei.shoupai.Remove(0);
                        }
                        timer_NeedChuPaiJiShi.Start();
                    }
                    if (pgID == desk_dong.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Gang)
                    {
                        desk_dong.shoupai.Remove(chupaishuzi);
                        desk_dong.shoupai.Remove(chupaishuzi);
                        desk_dong.shoupai.Remove(chupaishuzi);
                        desk_dong.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Gang, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_dong.shoupai.Contains(0))
                        {
                            desk_dong.shoupai.Remove(0);
                        }
                    }
                    else if (pgID == desk_nan.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Gang)
                    {
                        desk_nan.shoupai.Remove(chupaishuzi);
                        desk_nan.shoupai.Remove(chupaishuzi);
                        desk_nan.shoupai.Remove(chupaishuzi);
                        desk_nan.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Gang, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_nan.shoupai.Contains(0))
                        {
                            desk_nan.shoupai.Remove(0);
                        }
                    }
                    else if (pgID == desk_xi.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Gang)
                    {
                        desk_xi.shoupai.Remove(chupaishuzi);
                        desk_xi.shoupai.Remove(chupaishuzi);
                        desk_xi.shoupai.Remove(chupaishuzi);
                        desk_xi.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Gang, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_xi.shoupai.Contains(0))
                        {
                            desk_xi.shoupai.Remove(0);
                        }
                    }
                    else if (pgID == desk_bei.peer.player.actorId && list_pgReturn[0].pglx == PengGangLeixing.Gang)
                    {
                        desk_bei.shoupai.Remove(chupaishuzi);
                        desk_bei.shoupai.Remove(chupaishuzi);
                        desk_bei.shoupai.Remove(chupaishuzi);
                        desk_bei.cpg.Add(new CPGProperties() { cpglx = ChiPengGangLeixing.Gang, cpglist = new List<int>() { chupaishuzi, chupaishuzi, chupaishuzi, chupaishuzi } });
                        while (desk_bei.shoupai.Contains(0))
                        {
                            desk_bei.shoupai.Remove(0);
                        }
                    }
                }
                else if (list_chiReturn.Count > 0)
                {
                    Dictionary<short, object> Chidict = new Dictionary<short, object>();
                    int chipaiID = list_chiReturn[0].chiPaiActorID;
                    List<int> chipai = list_chiReturn[0].listchi;
                    if (desk_dong.peer.player.actorId == chipaiID)
                    {
                        for (int i = 0; i < list_chiReturn[0].listchi.Count; i++)
                        {
                            desk_dong.shoupai.Remove(list_chiReturn[0].listchi[i]);
                        }
                    }
                    else if (desk_nan.peer.player.actorId == chipaiID)
                    {
                        for (int i = 0; i < list_chiReturn[0].listchi.Count; i++)
                        {
                            desk_nan.shoupai.Remove(list_chiReturn[0].listchi[i]);
                        }
                    }
                    else if (desk_xi.peer.player.actorId == chipaiID)
                    {
                        for (int i = 0; i < list_chiReturn[0].listchi.Count; i++)
                        {
                            desk_xi.shoupai.Remove(list_chiReturn[0].listchi[i]);
                        }
                    }
                    else if (desk_bei.peer.player.actorId == chipaiID)
                    {
                        for (int i = 0; i < list_chiReturn[0].listchi.Count; i++)
                        {
                            desk_bei.shoupai.Remove(list_chiReturn[0].listchi[i]);
                        }
                    }
                    Chidict.Add(1, chupaishuzi);
                    Chidict.Add(2, chuPaiID);
                    Chidict.Add(3, list_chiReturn[0].chiPaiActorID);
                    list_chiReturn[0].listchi.Add(chupaishuzi);
                    chipai = list_chiReturn[0].listchi;
                    Chidict.Add(4, list_chiReturn[0].listchi);
                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (chipaiID == desklist[i].peer.player.actorId)
                        {
                            desklist[i].cpg.Add(new CPGProperties() { cpglist = chipai, cpglx = ChiPengGangLeixing.Chi });
                        }
                    }

                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (desklist[i].peer.IsConnected && desklist[i].peer != null)
                        {
                            desklist[i].peer.SendEvent(12, Chidict);
                        }
                    }
                    needChuPaiID = list_chiReturn[0].chiPaiActorID;
                    isganghouchupai = false;
                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (desklist[i].peer != null && desklist[i].peer.IsConnected)
                        {
                            desklist[i].peer.SendEvent(19, null);//开启计时功能
                        }
                    }
                    timer_NeedChuPaiJiShi.Start();
                }
                else
                {
                    Dictionary<short,object> dict=new Dictionary<short,object>();
                    dict.Add(1, chupaishuzi);
                    dict.Add(2, chuPaiID);

                    int nextmopaiID = 0;
                    if (chuPaiID != 0)
                    {
                        if (chuPaiID == desk_dong.peer.player.actorId)
                        {
                            nextmopaiID = desk_nan.peer.player.actorId;
                        }
                        else if(chuPaiID == desk_nan.peer.player.actorId)
                        {
                            nextmopaiID = desk_xi.peer.player.actorId;
                        }
                        else if (chuPaiID == desk_xi.peer.player.actorId)
                        {
                            nextmopaiID = desk_bei.peer.player.actorId;
                        }
                        else if (chuPaiID == desk_bei.peer.player.actorId)
                        {
                            nextmopaiID = desk_dong.peer.player.actorId;
                        }
                    }
                    dict.Add(3, nextmopaiID);
                    Dictionary<short, object> dict_daiLiMoPai = new Dictionary<short, object>();
                    dict_daiLiMoPai.Add(1, false);
                    dict_daiLiMoPai.Add(2, nextmopaiID);
                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (desklist[i].peer.player.actorId == nextmopaiID)
                        {
                            if (desklist[i].peer != null && desklist[i].peer.IsConnected)
                            {
                                Console.WriteLine(desklist[i].peer.player.username);
                                desklist[i].peer.SendEvent(8, dict);
                            }
                            else
                            {
                                for (int j = 0; j < desklist.Count; j++)
                                {
                                    if (desklist[j].peer != null && desklist[j].peer.IsConnected)
                                    {
                                        desklist[j].peer.SendEvent(9, dict_daiLiMoPai);
                                        Console.WriteLine("需要出牌的玩家掉线，代理摸牌");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                
                this.list_huReturn.Clear();
                this.list_chiReturn.Clear();
                this.list_pgReturn.Clear();
            }
        }
        public void QiangGangDataReturn()
        {
            if (isExcutedQiangGangDataReturn == false)
            {
                isExcutedQiangGangDataReturn = true;

                if (list_qiangGangReturn.Count > 0)
                {
                    Dictionary<short, object> dict = new Dictionary<short, object>();
                    dict.Add(1, anGangJiaGangID);
                    dict.Add(2, list_qiangGangReturn);
                    dict.Add(3,desk_dong.shoupai);
                    dict.Add(4,desk_nan.shoupai);
                    dict.Add(5, desk_xi.shoupai);
                    dict.Add(6, desk_bei.shoupai);
                    if (roomInfo.isYuePai)
                    {
                        JieSuan jiesuan = new JieSuan();
                        int lostFenshu = 0;
                        for (int i = 0; i < list_qiangGangReturn.Count; i++)
                        {
                            int tempfenshu = jiesuan.GetHuScore(roomInfo.roomType, list_qiangGangReturn[i].hplx_qiangGang, 1);

                            lostFenshu += tempfenshu;

                            int hupaiID = list_qiangGangReturn[i].huPaiActorID;

                            for (int j = 0; j < desklist.Count; j++)
                            {
                                if (desklist[j].peer.player.actorId == hupaiID)
                                {
                                    desklist[j].peer.player.jiFen += tempfenshu*3;
                                }
                            }
                        }

                        //2.再更新输玩家，或放炮玩家的分数
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            if (anGangJiaGangID == desklist[i].peer.player.actorId)
                            {
                                desklist[i].peer.player.jiFen -= lostFenshu*3;
                                break;
                            }
                        }

                        //保存每个桌子上的积分
                        desk_dong.jiFen = desk_dong.peer.player.jiFen;
                        desk_nan.jiFen = desk_nan.peer.player.jiFen;
                        desk_xi.jiFen = desk_xi.peer.player.jiFen;
                        desk_bei.jiFen = desk_bei.peer.player.jiFen;

                        int jiFen_dong = desk_dong.peer.player.jiFen;
                        int jiFen_nan = desk_nan.peer.player.jiFen;
                        int jiFen_xi = desk_xi.peer.player.jiFen;
                        int jiFen_bei = desk_bei.peer.player.jiFen;

                        dict.Add(7, jiFen_dong);
                        dict.Add(8, jiFen_nan);
                        dict.Add(9, jiFen_xi);
                        dict.Add(10, jiFen_bei);
                    }
                    else
                    {
                        JieSuan jiesuan = new JieSuan();
                        int lostFenshu = 0;
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            MySQLManager.UpdatejinriCoinJushu(1,desklist[i].peer.userinfo.id);
                            MySQLManager.UpdatejinriZongJushu(1,desklist[i].peer.userinfo.id);
                        }
                        for (int i = 0; i < list_qiangGangReturn.Count; i++)
                        {
                            int tempfenshu = jiesuan.GetHuScore(roomInfo.roomType, list_qiangGangReturn[i].hplx_qiangGang, 1);

                            lostFenshu += tempfenshu;

                            int hupaiID = list_qiangGangReturn[i].huPaiActorID;

                            for (int j = 0; j < desklist.Count; j++)
                            {
                                if (desklist[j].peer.player.actorId == hupaiID)
                                {
                                    int change = tempfenshu*3;
                                    MySQLManager.UpdateCoin(change, desklist[j].peer.userinfo.id);
                                }
                            }
                        }

                        //2.再更新输玩家，或放炮玩家的分数
                        for (int i = 0; i < desklist.Count; i++)
                        {
                            if (chuPaiID == desklist[i].peer.player.actorId)
                            {
                                int change = lostFenshu*3;
                                MySQLManager.UpdateCoin(-change, desklist[i].peer.userinfo.id);
                                break;
                            }
                        }

                        //保存每个桌子上的积分
                        desk_dong.jiFen = desk_dong.peer.player.jiFen;
                        desk_nan.jiFen = desk_nan.peer.player.jiFen;
                        desk_xi.jiFen = desk_xi.peer.player.jiFen;
                        desk_bei.jiFen = desk_bei.peer.player.jiFen;

                        int jiFen_dong = desk_dong.peer.player.jiFen;
                        int jiFen_nan = desk_nan.peer.player.jiFen;
                        int jiFen_xi = desk_xi.peer.player.jiFen;
                        int jiFen_bei = desk_bei.peer.player.jiFen;

                        int coin_dong = MySQLManager.UseIDfindCoin(desk_dong.peer.userinfo.id);
                        int coin_nan = MySQLManager.UseIDfindCoin(desk_nan.peer.userinfo.id);
                        int coin_xi = MySQLManager.UseIDfindCoin(desk_xi.peer.userinfo.id);
                        int coin_bei = MySQLManager.UseIDfindCoin(desk_bei.peer.userinfo.id);

                        dict.Add(11, coin_dong);
                        dict.Add(12, coin_nan);
                        dict.Add(13, coin_xi);
                        dict.Add(14, coin_bei);
                        
                        
                    }
                    dict.Add(15, roomInfo);
                    for (int i = 0; i < desklist.Count; i++)
                    {
                        //deskList[i].peer.gamestate = GameState.score;
                        if (desklist[i].peer.IsConnected && desklist[i].peer != null)
                        {
                            desklist[i].peer.SendEvent(18, dict);
                        }
                    }
                    OnGameOver();
                }
                else
                {
                    Dictionary<short, object> dict = new Dictionary<short, object>();
                    dict.Add(1, true);

                    for (int i = 0; i < desklist.Count; i++)
                    {
                        if (desklist[i].peer.player.actorId == anGangJiaGangID)
                        {
                            if (desklist[i].peer != null && desklist[i].peer.IsConnected)
                            {
                                desklist[i].peer.gamestate = GameState.score;
                                desklist[i].peer.SendEvent(8, dict);
                            }
                            else
                            {
                                //for (int j = 0; j < desklist.Count; j++)
                                //{
                                //    if (desklist[j].peer != null && desklist[j].peer.IsConnected)
                                //    {
                                //        desklist[j].peer.SendEvent((short)EventCode.daiLiMoPai, dict_daiLiMoPai);//也可以在这里直接摸牌出牌，把出牌数据发给其他人
                                //        Console.WriteLine("需要摸牌的玩家掉线，发送给其他在线玩家代理摸牌");
                                //        break;
                                //    }
                                //}
                            }
                            break;
                        }
                    }
                }
                list_qiangGangReturn.Clear();
            }
        }
        public void InitPai()
        {
            Queue<int> pai = new Queue<int>(136);
            Random ran = new Random();
            while (true)
            {
                int num = ran.Next(1, 137);
                if (!pai.Contains(num))
                {
                    pai.Enqueue(num);
                }
                if (pai.Count == 136)
                {
                    break;
                }
            }
            int count = pai.Count;
            for (int i = 0; i < count; i++)
            {
                if (pai.Count > 0)
                {
                    int num = pai.Dequeue();
                    PaiXingPaiShu pxps = GetPaiXingPaiShu(num);
                    queue_pai.Enqueue(pxps.GetHashCode());
                }
            }
        }
        public void OnGameOver()
        {
            //发送完胡牌结果后，应该检测一下房间中离线的玩家，把他们删除

            if (desk_dong.peer.IsConnected == false)
            {
                desk_dong.isJoin = false;
                //RoomPlayerDict.Remove(desk_dong.player.RoomActorID);
            }
            if (desk_nan.peer.IsConnected == false)
            {
                desk_nan.isJoin = false;
                //RoomPlayerDict.Remove(desk_nan.player.RoomActorID);
            }
            if (desk_xi.peer.IsConnected == false)
            {
                desk_xi.isJoin = false;
                //RoomPlayerDict.Remove(desk_xi.player.RoomActorID);
            }

            if (desk_bei.peer.IsConnected == false)
            {
                desk_bei.isJoin = false;
                //RoomPlayerDict.Remove(desk_bei.player.RoomActorID);
            }
            desk_dong.peer.player.isReady = false;
            desk_dong.shoupai.Clear();
            desk_dong.mopai = 0;
            desk_dong.throwpai.Clear();
            desk_dong.cpg.Clear();

            desk_nan.peer.player.isReady = false;
            desk_nan.shoupai.Clear();
            desk_nan.mopai = 0;
            desk_nan.throwpai.Clear();
            desk_nan.cpg.Clear();

            desk_xi.peer.player.isReady = false;
            desk_xi.shoupai.Clear();
            desk_xi.mopai = 0;
            desk_xi.throwpai.Clear();
            desk_xi.cpg.Clear();

            desk_bei.peer.player.isReady = false;
            desk_bei.shoupai.Clear();
            desk_bei.mopai = 0;
            desk_bei.throwpai.Clear();
            desk_bei.cpg.Clear();

        }
        public void OnHuangPai()
        {
            Dictionary<short, object> dict = new Dictionary<short, object>();
            dict.Add(1, roomInfo);

            for (int i = 0; i < desklist.Count; i++)
            {
                desklist[i].peer.gamestate = GameState.score;
                if (desklist[i].peer != null && desklist[i].peer.IsConnected)
                {
                    desklist[i].peer.SendEvent(22, dict);
                }
            }
            OnGameOver();
        }
        public PaiXingPaiShu GetPaiXingPaiShu(int num)
        {
            if (num >= 1 && num <= 4)
            {
                return PaiXingPaiShu.t1;
            }
            else if (num >= 5 && num <= 8)
            {
                return PaiXingPaiShu.t2;
            }
            else if (num >= 9 && num <= 12)
            {
                return PaiXingPaiShu.t3;
            }
            else if (num >= 13 && num <= 16)
            {
                return PaiXingPaiShu.t4;
            }
            else if (num >= 17 && num <= 20)
            {
                return PaiXingPaiShu.t5;
            }
            else if (num >= 21 && num <= 24)
            {
                return PaiXingPaiShu.t6;
            }
            else if (num >= 25 && num <= 28)
            {
                return PaiXingPaiShu.t7;
            }
            else if (num >= 29 && num <= 32)
            {
                return PaiXingPaiShu.t8;
            }
            else if (num >= 33 && num <= 36)
            {
                return PaiXingPaiShu.t9;
            }
            else if (num >= 37 && num <= 40)
            {
                return PaiXingPaiShu.b1;
            }
            else if (num >= 41 && num <= 44)
            {
                return PaiXingPaiShu.b2;
            }
            else if (num >= 45 && num <= 48)
            {
                return PaiXingPaiShu.b3;
            }
            else if (num >= 49 && num <= 52)
            {
                return PaiXingPaiShu.b4;
            }
            else if (num >= 53 && num <= 56)
            {
                return PaiXingPaiShu.b5;
            }
            else if (num >= 57 && num <= 60)
            {
                return PaiXingPaiShu.b6;
            }
            else if (num >= 61 && num <= 64)
            {
                return PaiXingPaiShu.b7;
            }
            else if (num >= 65 && num <= 68)
            {
                return PaiXingPaiShu.b8;
            }
            else if (num >= 69 && num <= 72)
            {
                return PaiXingPaiShu.b9;
            }
            else if (num >= 73 && num <= 76)
            {
                return PaiXingPaiShu.w1;
            }
            else if (num >= 77 && num <= 80)
            {
                return PaiXingPaiShu.w2;
            }
            else if (num >= 81 && num <= 84)
            {
                return PaiXingPaiShu.w3;
            }
            else if (num >= 85 && num <= 88)
            {
                return PaiXingPaiShu.w4;
            }
            else if (num >= 89 && num <= 92)
            {
                return PaiXingPaiShu.w5;
            }
            else if (num >= 93 && num <= 96)
            {
                return PaiXingPaiShu.w6;
            }
            else if (num >= 97 && num <= 100)
            {
                return PaiXingPaiShu.w7;
            }
            else if (num >= 101 && num <= 104)
            {
                return PaiXingPaiShu.w8;
            }
            else if (num >= 105 && num <= 108)
            {
                return PaiXingPaiShu.w9;
            }
            else if (num >= 109 && num <= 112)
            {
                return PaiXingPaiShu.df;
            }
            else if (num >= 113 && num <= 116)
            {
                return PaiXingPaiShu.nf;
            }
            else if (num >= 117 && num <= 120)
            {
                return PaiXingPaiShu.xf;
            }
            else if (num >= 121 && num <= 124)
            {
                return PaiXingPaiShu.bf;
            }
            else if (num >= 125 && num <= 128)
            {
                return PaiXingPaiShu.hz;
            }
            else if (num >= 129 && num <= 132)
            {
                return PaiXingPaiShu.fc;
            }
            else
            {
                return PaiXingPaiShu.bb;
            }
        }
        public void OnTimerOver_NeedChuPai(object sender, ElapsedEventArgs e)
        {
            int chuPaiShuZi = 0;
            bool isMoPai = false;

            for (int i = 0; i < desklist.Count; i++)
            {

                if (desklist[i].peer.player.actorId == needChuPaiID)
                {
                    //出牌
                    if (desklist[i].mopai != 0)
                    {
                        chuPaiShuZi = desklist[i].mopai;
                        isMoPai = true;
                    }
                    else
                    {
                        List<int> tempList = new List<int>(desklist[i].shoupai);
                        tempList.Sort();
                        tempList.Reverse();
                        chuPaiShuZi = tempList[0];
                        isMoPai = false;
                    }

                    this.chupaishuzi = chuPaiShuZi;
                    chuPaiID = needChuPaiID;

                    //出完牌后，各个玩家选择期间暂时不需要有人出牌，设置为0
                    needChuPaiID = 0;

                    //第一步，更新各自牌属性，第二步，检测胡、杠、碰、吃。                        
                    //if (isMoPai)
                    //{
                    //    desklist[i].mopai = 0;
                    //    desklist[i].throwpai.Add(chupaishuzi);
                    //}
                    if (isMoPai)//如果打的是摸得牌，就把这张牌添加到对应玩家的出牌堆里去，并且把这个玩家的摸牌数变成0
                    {
                        if (desk_dong.peer.player.actorId == chuPaiID)
                        {
                            desk_dong.throwpai.Add(chuPaiShuZi);
                            desk_dong.mopai = 0;
                            while (desk_dong.shoupai.Contains(0))
                            {
                                desk_dong.shoupai.Remove(0);
                            }
                        }
                        else if (desk_nan.peer.player.actorId == chuPaiID)
                        {
                            desk_nan.throwpai.Add(chuPaiShuZi);
                            desk_nan.mopai = 0;
                            while (desk_nan.shoupai.Contains(0))
                            {
                                desk_nan.shoupai.Remove(0);
                            }
                        }
                        else if (desk_xi.peer.player.actorId == chuPaiID)
                        {
                            desk_xi.throwpai.Add(chuPaiShuZi);
                            desk_xi.mopai = 0;
                            while (desk_xi.shoupai.Contains(0))
                            {
                                desk_xi.shoupai.Remove(0);
                            }
                        }
                        else if (desk_bei.peer.player.actorId == chuPaiID)
                        {
                            desk_bei.throwpai.Add(chuPaiShuZi);
                            desk_bei.mopai = 0;
                            while (desk_bei.shoupai.Contains(0))
                            {
                                desk_bei.shoupai.Remove(0);
                            }
                        }
                    }//判断打出来的牌的类型，把打出来的牌与玩家的手牌进行操作，同时放入弃牌堆
                    //else
                    //{
                    //    desklist[i].shoupai.Remove(chupaishuzi);
                    //    if (desklist[i].mopai != 0)
                    //    {
                    //        desklist[i].shoupai.Add(desklist[i].mopai);
                    //        desklist[i].mopai = 0;
                    //    }
                    //    desklist[i].throwpai.Add(chupaishuzi);
                    //}
                    else //如果打的不是摸的牌，得把这个牌从手牌里面删除出去
                    {
                        if (desk_dong.peer.player.actorId == chuPaiID)
                        {
                            desk_dong.throwpai.Add(chuPaiShuZi);
                            desk_dong.shoupai.Add(desk_dong.mopai);
                            desk_dong.shoupai.Remove(chuPaiShuZi);
                            desk_dong.mopai = 0;
                            while (desk_dong.shoupai.Contains(0))
                            {
                                desk_dong.shoupai.Remove(0);
                            }
                        }
                        else if (desk_nan.peer.player.actorId == chuPaiID)
                        {
                            desk_nan.throwpai.Add(chuPaiShuZi);
                            desk_nan.shoupai.Add(desk_nan.mopai);
                            desk_nan.shoupai.Remove(chuPaiShuZi);
                            desk_nan.mopai = 0;
                            while (desk_nan.shoupai.Contains(0))
                            {
                                desk_nan.shoupai.Remove(0);
                            }
                        }
                        else if (desk_xi.peer.player.actorId == chuPaiID)
                        {
                            desk_xi.throwpai.Add(chuPaiShuZi);
                            desk_xi.shoupai.Add(desk_xi.mopai);
                            desk_xi.shoupai.Remove(chuPaiShuZi);
                            desk_xi.mopai = 0;
                            while (desk_xi.shoupai.Contains(0))
                            {
                                desk_xi.shoupai.Remove(0);
                            }
                        }
                        else if (desk_bei.peer.player.actorId == chuPaiID)
                        {
                            desk_bei.throwpai.Add(chuPaiShuZi);
                            desk_bei.shoupai.Add(desk_bei.mopai);
                            desk_bei.shoupai.Remove(chuPaiShuZi);
                            desk_bei.mopai = 0;
                            while (desk_bei.shoupai.Contains(0))
                            {
                                desk_bei.shoupai.Remove(0);
                            }
                        }
                    }

                    isExcutedChuPaiDataReturn = false;

                    ////重置房间中4个桌子的出牌操作，出牌的人设置为true
                    ////当吃碰后的出牌必需设置开关，不然会出错（当其他家能吃碰杠时，下家会开始摸牌）
                    desk_dong.isChuPaiReturn = false;
                    desk_nan.isChuPaiReturn = false;
                    desk_xi.isChuPaiReturn = false;
                    desk_bei.isChuPaiReturn = false;
                    ////上面四个属性在摸牌时也被设置为false

                    desklist[i].isChuPaiReturn = true;

                    //为了防止玩家掉线重连后，没有正确返回
                    if (desk_dong.peer.IsConnected == false)
                    {
                        desk_dong.isChuPaiReturn = true;
                    }
                    if (desk_xi.peer.IsConnected == false)
                    {
                        desk_xi.isChuPaiReturn = true;
                    }
                    if (desk_nan.peer.IsConnected == false)
                    {
                        desk_nan.isChuPaiReturn = true;
                    }
                    if (desk_bei.peer.IsConnected == false)
                    {
                        desk_bei.isChuPaiReturn = true;
                    }

                    list_huReturn.Clear();
                    list_pgReturn.Clear();
                    list_chiReturn.Clear();

                    //第二步，检测胡、杠、碰、吃,发送给其余玩家
                    Dictionary<short, object> dict_ev = new Dictionary<short, object>();
                    dict_ev.Add(1, chupaishuzi);//告诉其他玩家出了哪张牌
                    dict_ev.Add(2, desklist[i].peer.player.actorId);//告诉其他玩家是谁出了牌

                    //发送的响应dict中包含1.牌的数字，2.胡的类型，3.吃的类型（list，如果list。count为0 ，则不吃），4.碰杠的类型：碰、可碰可杠
                    if (desklist[i].peer.player.playerPos == PlayerPosition.dong)
                    {
                        Hupaileixing hplx_nan = jianCe.JianCeHuPaiLeiXing(desk_nan, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_nan = jianCe.JiancePenggangLeixing(desk_nan, chupaishuzi);
                        List<int> listchi_nan = jianCe.JianCeXiajiachi(desk_nan, chupaishuzi);
                        Dictionary<short, object> paileiXing_nan = new Dictionary<short, object>();
                        paileiXing_nan.Add(1, hplx_nan);
                        paileiXing_nan.Add(2, pglx_nan);
                        paileiXing_nan.Add(3, listchi_nan);
                        paileiXing_nan.Add(4, chupaishuzi);
                        if (desk_nan.peer != null && desk_nan.peer.IsConnected)
                        {
                            desk_nan.peer.SendEvent(7, paileiXing_nan);
                        }
                        

                        Hupaileixing hplx_xi = jianCe.JianCeHuPaiLeiXing(desk_xi, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_xi = jianCe.JiancePenggangLeixing(desk_xi, chupaishuzi);
                        List<int> listchi_xi = null;
                        Dictionary<short, object> paileiXing_xi = new Dictionary<short, object>();
                        paileiXing_xi.Add(1, hplx_xi);
                        paileiXing_xi.Add(2, pglx_xi);
                        paileiXing_xi.Add(3, listchi_xi);
                        paileiXing_xi.Add(4, chupaishuzi);
                        if (desk_xi.peer != null && desk_xi.peer.IsConnected)
                        {
                            desk_xi.peer.SendEvent(7, paileiXing_xi);
                        }
                        

                        Hupaileixing hplx_bei = jianCe.JianCeHuPaiLeiXing(desk_bei, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_bei = jianCe.JiancePenggangLeixing(desk_bei, chupaishuzi);
                        List<int> listchi_bei = null;
                        Dictionary<short, object> paileiXing_bei = new Dictionary<short, object>();
                        paileiXing_bei.Add(1, hplx_bei);
                        paileiXing_bei.Add(2, pglx_bei);
                        paileiXing_bei.Add(3, listchi_bei);
                        paileiXing_bei.Add(4, chupaishuzi);
                        if (desk_bei.peer != null && desk_bei.peer.IsConnected)
                        {
                            desk_bei.peer.SendEvent(7, paileiXing_bei);
                        }
                        
                    }  //判断刚刚出牌的人是谁，让除他之外的所有人看看自己能否胡牌，碰牌，杠牌
                    else if (desklist[i].peer.player.playerPos == PlayerPosition.nan)
                    {
                        Hupaileixing hplx_xi = jianCe.JianCeHuPaiLeiXing(desk_xi, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_xi = jianCe.JiancePenggangLeixing(desk_xi, chupaishuzi);
                        List<int> listchi_xi = jianCe.JianCeXiajiachi(desk_xi, chupaishuzi);
                        Dictionary<short, object> paileiXing_xi = new Dictionary<short, object>();
                        paileiXing_xi.Add(1, hplx_xi);
                        paileiXing_xi.Add(2, pglx_xi);
                        paileiXing_xi.Add(3, listchi_xi);
                        paileiXing_xi.Add(4, chupaishuzi);
                        if (desk_xi.peer != null && desk_xi.peer.IsConnected)
                        {
                            desk_xi.peer.SendEvent(7, paileiXing_xi);
                        }
                        

                        Hupaileixing hplx_bei = jianCe.JianCeHuPaiLeiXing(desk_bei, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_bei = jianCe.JiancePenggangLeixing(desk_bei, chupaishuzi);
                        List<int> listchi_bei = null;
                        Dictionary<short, object> paileiXing_bei = new Dictionary<short, object>();
                        paileiXing_bei.Add(1, hplx_bei);
                        paileiXing_bei.Add(2, pglx_bei);
                        paileiXing_bei.Add(3, listchi_bei);
                        paileiXing_bei.Add(4, chupaishuzi);
                        if (desk_bei.peer != null && desk_bei.peer.IsConnected)
                        {
                            desk_bei.peer.SendEvent(7, paileiXing_bei);
                        }
                        

                        Hupaileixing hplx_dong = jianCe.JianCeHuPaiLeiXing(desk_dong, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_dong = jianCe.JiancePenggangLeixing(desk_dong, chupaishuzi);
                        List<int> listchi_dong = null;
                        Dictionary<short, object> paileiXing_dong = new Dictionary<short, object>();
                        paileiXing_dong.Add(1, hplx_dong);
                        paileiXing_dong.Add(2, pglx_dong);
                        paileiXing_dong.Add(3, listchi_dong);
                        paileiXing_dong.Add(4, chupaishuzi);
                        if (desk_dong.peer != null && desk_dong.peer.IsConnected)
                        {
                            desk_dong.peer.SendEvent(7, paileiXing_dong);
                        }
                        
                    }
                    else if (desklist[i].peer.player.playerPos == PlayerPosition.xi)
                    {
                        Hupaileixing hplx_bei = jianCe.JianCeHuPaiLeiXing(desk_bei, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_bei = jianCe.JiancePenggangLeixing(desk_bei, chupaishuzi);
                        List<int> listchi_bei = jianCe.JianCeXiajiachi(desk_bei, chupaishuzi);
                        Dictionary<short, object> paileiXing_bei = new Dictionary<short, object>();
                        paileiXing_bei.Add(1, hplx_bei);
                        paileiXing_bei.Add(2, pglx_bei);
                        paileiXing_bei.Add(3, listchi_bei);
                        paileiXing_bei.Add(4, chupaishuzi);
                        if (desk_bei.peer != null && desk_bei.peer.IsConnected)
                        {
                            desk_bei.peer.SendEvent(7, paileiXing_bei);
                        }
                        

                        Hupaileixing hplx_dong = jianCe.JianCeHuPaiLeiXing(desk_dong, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_dong = jianCe.JiancePenggangLeixing(desk_dong, chupaishuzi);
                        List<int> listchi_dong = null;
                        Dictionary<short, object> paileiXing_dong = new Dictionary<short, object>();
                        paileiXing_dong.Add(1, hplx_dong);
                        paileiXing_dong.Add(2, pglx_dong);
                        paileiXing_dong.Add(3, listchi_dong);
                        paileiXing_dong.Add(4, chupaishuzi);
                        if (desk_dong.peer != null && desk_dong.peer.IsConnected)
                        {
                            desk_dong.peer.SendEvent(7, paileiXing_dong);
                        }
                        

                        Hupaileixing hplx_nan = jianCe.JianCeHuPaiLeiXing(desk_nan, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_nan = jianCe.JiancePenggangLeixing(desk_nan, chupaishuzi);
                        List<int> listchi_nan = null;
                        Dictionary<short, object> paileiXing_nan = new Dictionary<short, object>();
                        paileiXing_nan.Add(1, hplx_nan);
                        paileiXing_nan.Add(2, pglx_nan);
                        paileiXing_nan.Add(3, listchi_nan);
                        paileiXing_nan.Add(4, chupaishuzi);
                        if (desk_nan.peer != null && desk_nan.peer.IsConnected)
                        {
                            desk_nan.peer.SendEvent(7, paileiXing_nan);
                        }
                        
                    }
                    else if (desklist[i].peer.player.playerPos == PlayerPosition.bei)
                    {
                        Hupaileixing hplx_dong = jianCe.JianCeHuPaiLeiXing(desk_dong, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_dong = jianCe.JiancePenggangLeixing(desk_dong, chupaishuzi);
                        List<int> listchi_dong = jianCe.JianCeXiajiachi(desk_dong, chupaishuzi);
                        Dictionary<short, object> paileiXing_dong = new Dictionary<short, object>();
                        paileiXing_dong.Add(1, hplx_dong);
                        paileiXing_dong.Add(2, pglx_dong);
                        paileiXing_dong.Add(3, listchi_dong);
                        paileiXing_dong.Add(4, chupaishuzi);
                        if (desk_dong.peer != null && desk_dong.peer.IsConnected)
                        {
                            desk_dong.peer.SendEvent(7, paileiXing_dong);
                        }
                        

                        Hupaileixing hplx_nan = jianCe.JianCeHuPaiLeiXing(desk_nan, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_nan = jianCe.JiancePenggangLeixing(desk_nan, chupaishuzi);
                        List<int> listchi_nan = null;
                        Dictionary<short, object> paileiXing_nan = new Dictionary<short, object>();
                        paileiXing_nan.Add(1, hplx_nan);
                        paileiXing_nan.Add(2, pglx_nan);
                        paileiXing_nan.Add(3, listchi_nan);
                        paileiXing_nan.Add(4, chupaishuzi);
                        if (desk_nan.peer != null && desk_nan.peer.IsConnected)
                        {
                            desk_nan.peer.SendEvent(7, paileiXing_nan);
                        }
                        

                        Hupaileixing hplx_xi = jianCe.JianCeHuPaiLeiXing(desk_xi, chupaishuzi, isganghouchupai, false);
                        PengGangLeixing pglx_xi = jianCe.JiancePenggangLeixing(desk_xi, chupaishuzi);
                        List<int> listchi_xi = null;
                        Dictionary<short, object> paileiXing_xi = new Dictionary<short, object>();
                        paileiXing_xi.Add(1, hplx_xi);
                        paileiXing_xi.Add(2, pglx_xi);
                        paileiXing_xi.Add(3, listchi_xi);
                        paileiXing_xi.Add(4, chupaishuzi);
                        if (desk_xi.peer != null && desk_xi.peer.IsConnected)
                        {
                            desk_xi.peer.SendEvent(7, paileiXing_xi);
                        }
                        
                    }

                    ////发送一个事件给房间中的所有人开启10秒计时  
                    //for (int j = 0; j < desklist.Count; j++)
                    //{
                    //    if (desklist[j].peer != null && desklist[j].peer.IsConnected)
                    //    {
                    //        desklist[j].peer.SendEvent(19, null);
                    //    }
                    //}

                    Dictionary<short, object> PaiMessage = new Dictionary<short, object>();
                    PaiMessage.Add(1, chuPaiID);
                    PaiMessage.Add(2, chupaishuzi);
                    PaiMessage.Add(3, isMoPai);
                    for (int j = 0; j < desklist.Count; j++)
                    {
                        if (desklist[j].peer != null && desklist[j].peer.IsConnected)
                        {
                            desklist[j].peer.SendToOther(6, PaiMessage); ;
                        }
                    }
                    //开启房间内出牌返回计时
                    timer_ChuPaiReturnJiShi.Start();
                    break;
                }
            }
        }

        public void OnTimerOver_ChuPaiReturn(object sender, ElapsedEventArgs e)
        {
            desk_dong.isChuPaiReturn = true;
            desk_nan.isChuPaiReturn = true;
            desk_xi.isChuPaiReturn = true;
            desk_bei.isChuPaiReturn = true;
            ChuPaiDataReturn();
        }
        public void OnTimerOver_QiangGangReturn(object sender, ElapsedEventArgs e)
        {
            desk_dong.isQiangGangReturn = true;
            desk_nan.isQiangGangReturn = true;
            desk_xi.isQiangGangReturn = true;
            desk_bei.isQiangGangReturn = true;
            QiangGangDataReturn();
        }

        private bool disposedValue;
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    //component.Dispose();

                }

                // TODO: 释放未托管的资源(未托管的对象)并替代终结器
                // TODO: 将大型字段设置为 null


                timer_ChuPaiReturnJiShi.Dispose();
                timer_NeedChuPaiJiShi.Dispose();
                timer_QiangGangReturnJiShi.Dispose();
                timer_updatePai.Dispose();


                desk_dong.Dispose();
                desk_nan.Dispose();
                desk_xi.Dispose();
                desk_bei.Dispose();


                list_qiangGangReturn.Clear();
                list_chiReturn.Clear();
                list_pgReturn.Clear();
                list_huReturn.Clear();
                list_qiangGangReturn = null;
                list_chiReturn = null;
                list_pgReturn = null;
                list_huReturn = null;

                desklist.Clear();
                desklist = null;

                roomInfo = null;
                queue_pai.Clear();
                queue_pai = null;

                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        //~Room()
        //{
        //    //不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //    Dispose(disposing: false);
        //    Console.WriteLine("调用了room的析构方法");

        //}

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            Console.WriteLine("调用了room的Dispose（）方法");
            //LOG.Log("调用了room的Dispose（）方法");
            //GC.SuppressFinalize(this);
        }

    }
    public class Desk: IDisposable
    {
        public bool isJoin = false;
        public bool isChuPaiReturn = true;
        public bool isQiangGangReturn = false;
        public PlayerPosition playerPosition=PlayerPosition.dong;
        public Peer peer = null;
        public List<int> shoupai = new List<int>();
        public int mopai = 0;
        public int jiFen = 0;
        public List<int> throwpai = new List<int>();
        public List<CPGProperties> cpg = new List<CPGProperties>();

        private bool disposedValue;
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                }

                // TODO: 释放未托管的资源(未托管的对象)并替代终结器
                // TODO: 将大型字段设置为 null

                peer = null;
                shoupai.Clear();
                shoupai = null;
                throwpai.Clear();
                throwpai = null;
                cpg.Clear();
                cpg = null;

                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        //~Desk()
        //{
        //    // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //    Dispose(disposing: false);
        //}

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            //GC.SuppressFinalize(this);
        }
    }
    public class RoomInfo
    {
        public int yuePaiJuShu = 4;//是约牌创建时定下的总局数
        public int duiZhanJuShu = 0;
        public bool iskechi = true;
        public int roomName = 0;
        public bool isYuePai = true;
        public RoomType roomType = RoomType.tongyong;
        public int masterClientID = 0;
        public GameLevel gameLevel = GameLevel.yupai;
    }
    public class DeskInfo
    {
        public List<int> shoupai = new List<int>();//手牌
        public int mopai = 0;
        public List<int> throwPai = new List<int>();//抛牌
        public List<CPGProperties> cpg = new List<CPGProperties>();//吃碰杠牌
    }
}
public class CPGProperties
{
    public ChiPengGangLeixing cpglx;
    public List<int> cpglist;
}

public class HuReturnData
{
    public int hupaiActorID = 0;
    public Hupaileixing hplx = Hupaileixing.buhu;
}
public class PengGangReturnData
{
    public int pengGangActorID = 0;
    public PengGangLeixing pglx = PengGangLeixing.BupengBuGang;
}
public class QiangGangReturnData
{
    public int huPaiActorID = 0;
    public Hupaileixing hplx_qiangGang = Hupaileixing.buhu;
}
public class ChiReturnData
{
    public int chiPaiActorID = 0;
    public List<int> listchi = new List<int>();
}
